package sugarbind_input

class Ligand {

	String full_Name
	String abbrev_Name
	String ligand_Notes
	String ligand_Ref

	
	static constraints = {
		full_Name(nullable:true)
		abbrev_Name(nullable:true)
		
		structure(nullable:false)
//		ligand_Type(nullable:false)
		
		glycoconjugate_Type(nullable:true)
		aglycon(nullable:true)
		
		ligand_Synonyms(nullable:true)
		ligand_Notes(nullable:true)
		ligand_Ref(nullable:true)
		sources(nullable:true)
		
		full_Name(unique:['structure','abbrev_Name'])
	}
	
	static hasOne = [structure:Structure, glycoconjugate_Type:Glycoconjugate_Type, aglycon:Aglycon]
	static hasMany = [ligand_Types:Ligand_Type, ligand_Synonyms:Ligand_Synonym, bindings:Binding, sources:Source ]
//	static belongsTo = Ligand_Type
	
	static mapping = {
//		id column: 'ligand_id'
		id generator: 'sequence', params: [sequence: 'ligand_id_seq']
		
		sources joinTable: [name: "ligand_source", key: 'ligand_id' ]
		ligand_Types joinTable: [name: "ligand_ligand_type", key: 'ligand_id' ]
		
	}
	
	public String toString(){
		def ligand
		if(full_Name != null && !full_Name.isEmpty())
		{
			ligand = full_Name
		}
		else if(abbrev_Name != null && !abbrev_Name.isEmpty())
		{
			ligand = abbrev_Name
		}
		else
		{
			ligand = " "
		}
		 ligand + " / " + structure.iupac + " (" + id + ")" 
	}
	
	public static listLigands()
	{
		Ligand.list().sort{ it.toString(); }
	}
}
